<template>
  <transition name="fade">
    <div class="input-case" v-drag>
      <img src="../../assets/image/logo.png" alt=".." class="logo">
      <svg class="icon icon-guanbi" aria-hidden="true" @click="OffLogin">
        <use xlink:href="#icon-guanbi"></use>
      </svg>
      <div class="input-title">
        <svg class="icon icon-btn" aria-hidden="true" @click="ToLogin">
          <use :xlink:href="TagAttr.login"></use>
        </svg>
        <svg class="icon icon-btn" aria-hidden="true" @click="ToRegister">
          <use :xlink:href="TagAttr.register"></use>
        </svg>
      </div>
      <div @mousedown.stop>
        <transition name="login">
          <login @changeLogin="login" v-if="flag" ref="login"></login>
        </transition>
        <transition name="register">
          <register @changeRegister="register" v-if="!flag"></register>
        </transition>
      </div>
    </div>
  </transition>
</template>

<script>
import { mapActions, mapMutations } from 'vuex';
import Login from './login.vue'
import Register from './register.vue'
export default {
  name: 'Loginregister',

  data() {
    return {
      flag: true, //控制登录注册表单切换
      TagAttr: { //标题图标切换
        login: '#icon-denglu1',
        register: '#icon-zhuce'
      }
    };
  },

  methods: {
    //关闭登录框
    OffLogin(){
      this.$emit('OffLogin')
    },
    //跳转登录表单
    ToLogin(){
      this.$refs.login ? this.$refs.login.isPage = 0 : ''
      this.flag = true
      this.TagAttr.login = '#icon-denglu1'
      this.TagAttr.register = '#icon-zhuce'
    },
    //跳转注册表单
    ToRegister(){
      this.flag = false
      this.TagAttr.login = '#icon-denglu'
      this.TagAttr.register = '#icon-zhucebule'
    },
    // 注册
    register(registerInfo){
      this.$api.toRegister(registerInfo).then(res => {
        if(res == 1){
          this.showHint({title:'注册成功',isFlag:true})
          this.ToLogin()
        }else{
          this.showHint({title:res,isFlag:false})
        }
      })
    },
    // 登陆
    login(loginInfo){
      this.$api.toLogin({username:loginInfo.userName,password:loginInfo.password}).then(res => {
        if(res instanceof Object){
          this.getUserInfo(res)
          this.OffLogin()
          this.showHint({title: '登录成功',isFlag: true})
        }else{
          this.showHint({title: res,isFlag: false})
        }
      })
    },
    ...mapActions(['showHint']),
    ...mapMutations(['getUserInfo'])
  },

  components: {
    Login,
    Register,
  }
};
</script>

<style>
.input-case{
  position: fixed;
  top: 50%;
  left: 50%;
  width: 400px;
  height: 500px;
  border: 1px solid gray;
  transform: translate(-50%,-50%);
  background-color: #fff;
  box-shadow: 0 0 15px gray;
  text-align: center;
  background-image: url('../../assets/image/loginBg.jpeg');
  background-size: cover;
  overflow: hidden;
  border-radius: 10px;
  z-index: 999;
}
.input-case .logo{
  width: 66px;
  height: 66px;
  position: absolute;
  top: 20px;
  left: 20px;
}
.input-title{
  text-align: center;
  margin-top: 50px;
}
.icon-btn{
  font-size: 66px;
  cursor: pointer;
}
.icon-guanbi{
  position: absolute;
  top: 20px;
  right: 20px;
  font-size: 35px;
  cursor: pointer;
}
.input-case input{
  outline: none;
  border: 1px solid gray;
  display: block;
  width: 300px;
  height: 40px;
  font-size: 16px;
  padding-left: 10px;
  margin-top: 20px;
}
.input-case .btn{
  display: inline-block;
  width: 100px;
  height: 40px;
  cursor: pointer;
  background: -webkit-linear-gradient(left, #ff00f2, #04ffc0);
  border-radius: 25px;
  margin-top: 30px;
  opacity: .7s;
  transition: all .5s;
  background-size: 200%;
  background-position: 0%,0%;
  font-size: 18px;
  letter-spacing: 5px;
  color: #fff;
}
.input-case .btn:hover{
  opacity: 1;
  background-position: 100%,100%;
}
.input-case form{
  width: 300px;
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}
input.verification{
  width: 200px;
  display: inline-block;
}
.code{
  display: inline-block;
  width: 80px;
  height: 38px;
  vertical-align: bottom;
  border: 1px solid gray;
  cursor: pointer;
  margin-left: 20px;
  background-color: #fff;
}
/* 登录框动画 */
.login-enter,.login-leave-to{
  transform: translateX(-300px);
  opacity: 0;
}
.login-enter-active,.login-leave-active{
  transition: all .5s ease;
}
.login-enter-to,.login-leave{
  transform: translateX(0);
  opacity: 1;
}

/* 注册框动画 */
.register-enter,.register-leave-to{
  transform: translateX(300px);
  opacity: 0;
}
.register-enter-active,.register-leave-active{
  transition: all .5s ease;
}
.register-enter-to,.register-leave{
  transform: translateX(0);
  opacity: 1;
}
@media screen and (max-width: 800px) {
  .input-case{
    width: 320px;
    top: 20%;
    transform: translateX(-50%);
  }
}
</style>